extends Node2D

# 手牌扇形绘图区尺寸设定
# 扇形区角度
const HAND_REGION_TOTAL_ANGLE: int = 10
# 扇形区半径
const HAND_REGION_SECTOR_RADIUS: int = 3000
# 扇形区最大步长
const HAND_REGION_SECTOR_MAX_ANGLE_STEP: int = 2
# 卡牌固定高度
const CARD_HEIGHT: int = 200
# 卡牌固定宽度
const CARD_WIDTH: int = 140
# 手牌区中心点离屏幕底部距离
const HAND_REGION_HEIGHT: int = 50
# 手牌区重排动画持续时间
const HAND_CARD_ARRANGE_ANIMATION_TIME: float = 0.3


# 卡牌
var cards: Array = []

# 滑动控制节点
onready var tween_controller: Tween = get_node("TweenController")
# 大卡牌显示节点

# 前一帧选择的卡牌
var selected_card_pos: int = -1

# 信号
signal card_selected
signal card_unselected


# 变量
var last_focus_pos_rad: float = 0
var last_focus_distance_to_center: float = 0

func _ready() -> void:
    # 设置参数
    set_process_unhandled_input(true)
    set_process(true)
    pass


func add_card(c: Card, pos: int=-1) -> void:
    if pos == -1:
        pos = cards.size()
    if pos < 0 or pos > cards.size():
        return

    var size: Vector2 = get_viewport().get_size()
    c.set_position(Vector2(size.x, 0))
    cards.insert(pos, c)
    add_child(c)
    _rearange_card()
    pass


func _process(delta):
    if cards.size() == 0:
        return

    var s: int = cards.size()
    var center: Vector2 = position + Vector2(0, HAND_REGION_SECTOR_RADIUS)
    var rad_per_card: float = float(CARD_WIDTH) / HAND_REGION_SECTOR_RADIUS
    var current_pos: int = -1
    for i in range(cards.size()):
        var card_pos: Vector2 = cards[cards.size() - 1 - i].global_position
        var card_pos_rad: float = 0.0
        var card_distance: float = center.distance_to(card_pos)
        if card_pos.x - center.x < 0:
            card_pos_rad = PI - atan((card_pos.y - center.y) / (card_pos.x - center.x))
        elif card_pos.x - center.x > 0:
            card_pos_rad = - atan((card_pos.y - center.y) / (card_pos.x - center.x))
        else:
            card_pos_rad = PI / 2
        pass

        var h_in_card: bool = last_focus_pos_rad < card_pos_rad + rad_per_card / 2 and last_focus_pos_rad > card_pos_rad - rad_per_card / 2
        var v_in_card: bool = last_focus_distance_to_center < card_distance + CARD_HEIGHT / 2 and last_focus_distance_to_center > card_distance - CARD_HEIGHT / 2
        if h_in_card and v_in_card:
            current_pos = cards.size() - 1 - i
            break
        pass

    if current_pos == -1:
        # 当前不在任何卡牌区，如果有选中卡牌取消，没有就什么都不做
        if selected_card_pos != -1:
            emit_signal('card_unselected', selected_card_pos, cards[selected_card_pos])
            selected_card_pos = -1
        else:
            pass
    else:
        # 当前选中一个卡牌，如果和之前卡牌一样，什么都不做，否则重选卡牌信号
        if selected_card_pos == current_pos:
            pass
        else:
            # 如果之前有选择卡牌，需要取消
            if selected_card_pos != -1:
                emit_signal('card_unselected', selected_card_pos, cards[selected_card_pos])
            selected_card_pos = current_pos
            emit_signal('card_selected', current_pos, cards[current_pos])
            pass
        pass
    pass


func _unhandled_input(event):
    # 鼠标移动事件
    if event is InputEventMouseMotion:
        # 计算关注点弧度
        var center: Vector2 = position + Vector2(0, HAND_REGION_SECTOR_RADIUS)
        var rad_per_card: float = float(CARD_WIDTH) / HAND_REGION_SECTOR_RADIUS
        last_focus_distance_to_center = center.distance_to(event.position)
        if event.position.x - center.x < 0:
            last_focus_pos_rad = PI - atan((event.position.y - center.y) / (event.position.x - center.x))
        elif event.position.x - center.x > 0:
            last_focus_pos_rad = - atan((event.position.y - center.y) / (event.position.x - center.x))
        else:
            last_focus_pos_rad = PI / 2
    pass


func _rearange_card() -> void:
    var s: int = cards.size()
    if s == 1:
        tween_controller.interpolate_property(
            cards[0],
            "position",
            cards[0].get_position(),
            Vector2(0, 0),
            HAND_CARD_ARRANGE_ANIMATION_TIME,
            Tween.TRANS_LINEAR,
            Tween.EASE_IN)
        tween_controller.interpolate_property(
            cards[0],
            "rotation",
            cards[0].rotation,
            0,
            HAND_CARD_ARRANGE_ANIMATION_TIME,
            Tween.TRANS_LINEAR,
            Tween.EASE_IN)
        pass
    else:
        var angle_step: float = float(HAND_REGION_TOTAL_ANGLE) / (s - 1)
        if angle_step > HAND_REGION_SECTOR_MAX_ANGLE_STEP:
            angle_step = HAND_REGION_SECTOR_MAX_ANGLE_STEP
        var rad_step: float = deg2rad(angle_step)
        var start_rad: float = PI / 2 + rad_step * ((s - 1) / 2.0)
        for i in range(s):
            var rad: float = start_rad - i * rad_step
            var x: int = int(cos(rad) * HAND_REGION_SECTOR_RADIUS)
            var y: int = int(sin(rad) * HAND_REGION_SECTOR_RADIUS)
            var c: Card = cards[i]
            tween_controller.interpolate_property(
                c,
                "position",
                c.get_position(),
                Vector2(x, HAND_REGION_SECTOR_RADIUS - y),
                HAND_CARD_ARRANGE_ANIMATION_TIME,
                Tween.TRANS_LINEAR,
                Tween.EASE_IN)
            tween_controller.interpolate_property(
                c,
                "rotation",
                c.rotation,
                PI / 2 - rad,
                HAND_CARD_ARRANGE_ANIMATION_TIME,
                Tween.TRANS_LINEAR,
                Tween.EASE_IN)
            pass
    tween_controller.start()
    pass
